﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using System.Configuration;

namespace SistemaPetShop.Negocio
{
    class Agenda
    {
        Conexao c = new Conexao();
        MySqlDataReader dataReaderMySql;
        StringBuilder query = new StringBuilder();
        EventLog log = new EventLog();
        BaseClass baseClass = new BaseClass();

        public DataTable _ListaHistoricoCliente(Int32 idCliente,Int32 idAnimal)
        {
            DataTable dt = new DataTable();
            c.AbrirConexao();


            query.Append(" select a.Data,a.Valor,a.Procedimento,a.Observacao,b.nome,c.Apelido ");
            query.Append(" from agenda a");
            query.Append(" join cliente b ");
            query.Append(" on a.Cliente_idCliente = b.idcliente ");
            query.Append(" join animal c ");
            query.Append(" on a.animal_idAnimal = c.idAnimal ");
            query.Append(" where a.Cliente_idCliente = ");
            query.Append(idCliente.ToString());
            query.Append(" and a.animal_idAnimal = ");
            query.Append(idAnimal.ToString());
            query.Append(" order by a.data desc ");
            dataReaderMySql = c.Consulta(query.ToString());
            query.Remove(0, query.Length);
            dt.Load(dataReaderMySql);
            c.FecharConexao();
            return dt;

        }

        public DataTable _ListaAgenda()
        {
            DataTable dt = new DataTable();
            c.AbrirConexao();

            query.Append("select distinct ag.data ,ag.data_final,ag.procedimento,ag.observacao,ag.valor,cl.nome,an.apelido ");
            query.Append("from Agenda ag ");
            query.Append("join Cliente cl ");
            query.Append("on ag.Cliente_idCliente = cl.idCliente ");
            query.Append("join Animal an ");
            query.Append("on ag.Animal_idAnimal = an.idAnimal; ");
            dataReaderMySql = c.Consulta(query.ToString());
            dt.Load(dataReaderMySql);
            query.Remove(0, query.Length);
            c.FecharConexao();
            return dt;
        }

        public DataTable _ListaAgendaParaApagar()
        {
            DataTable dt = new DataTable();
            c.AbrirConexao();

            query.Append("select distinct cl.nome,an.apelido,ag.data ,ag.data_final,ag.procedimento,ag.observacao,ag.valor,ag.Cliente_idCliente,ag.Animal_idAnimal ");
            query.Append("from Agenda ag ");
            query.Append("join Cliente cl ");
            query.Append("on ag.Cliente_idCliente = cl.idCliente ");
            query.Append("join Animal an ");
            query.Append("on ag.Animal_idAnimal = an.idAnimal ");
            query.Append(" where ag.data >= CURRENT_DATE() ");
            query.Append(" order by 3 ;");
            dataReaderMySql = c.Consulta(query.ToString());
            dt.Load(dataReaderMySql);
            query.Remove(0, query.Length);
            c.FecharConexao();
            return dt;
        }


        public bool _ApagaAgenda(Int32 idCliente, Int32 idAnimal, DateTime data)
        {
            bool ret = false;

            try
            {
                c.AbrirConexao();
                query.Append(" DELETE FROM Agenda where Cliente_idCliente = ");
                query.Append(idCliente.ToString());
                query.Append(" and Animal_idAnimal = ");
                query.Append(idAnimal.ToString());
                query.Append(" and data = '");
                query.Append(data.ToString("yyyy/MM/dd HH:mm:sss") + "'");

                ret = c.Insert(query.ToString());

                query.Remove(0, query.Length);

                c.FecharConexao();

                return ret;


            }
            catch (Exception ex)
            {
                c.FecharConexao();
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
                return ret;                
                throw;
            }
        }


        public bool _InsereNovaAgenda(DateTime data, String procedimento, String observacao, String valor, Int32 idCliente, Int32 idAnimal,DateTime dataFinal)
        {
            bool ret = false;
            try
            {
                
                c.AbrirConexao();
                query.Append("INSERT INTO Agenda (data,data_final, procedimento,observacao,valor,Cliente_idCliente,Animal_idAnimal) VALUES ( '");
                query.Append(data.ToString("yyyy/MM/dd HH:mm:ss.fff") + "','");
                query.Append(dataFinal.ToString("yyyy/MM/dd HH:mm:ss.fff") + "','");
                query.Append(procedimento + "','");
                query.Append(observacao + "','");
                query.Append(baseClass._FormataValorInsereBanco(valor) + "','");
                query.Append(idCliente.ToString() + "','");
                query.Append(idAnimal.ToString() + "')");


                ret = c.Insert(query.ToString());
                query.Remove(0, query.Length);

                c.FecharConexao();
                return ret;

            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
                return ret;
                throw;
            }
        }
    
    }
}
